<?php

chdir(dirname(__FILE__)."/../..");
$path = getcwd();
chdir(dirname(__FILE__));

include_once $path."/../lib/core.class.php";


class HistoryProducts extends IPlugin  
{

// ----------------------------------------------------------------------------------
// --- [ Constructor ] -------------------------------------------------------------
// ----------------------------------------------------------------------------------

    function HistoryProducts($parent = null)
    {
        IPlugin::IPlugin($parent);
        $this->tplfolder = dirname(__FILE__)."/tpl";
    }

    function getPluginTag() { return "historyproducts"; }
    function getPluginName() { return "Create History Last Visit Products defined data snippet"; }
    function getPluginVersion() { return "1.0.0"; }

// ----------------------------------------------------------------------------------
// --- [ Insert history record ] -------------------------------------------------------------
// ----------------------------------------------------------------------------------

function insertHistoryRecord($arr_params = array())
{
    /*$arr_params = array(
     * prod_id = 0;  if prod_id = 0 or empty - default select information all products
     * limit_records = 0; - limit records in database; if limit_records = 0 or empty - default limit_records = 50
     * user_id = 0;  if user_id = 0 or empty - function do not work
     * )
     */



    $prod_id = (!isset($arr_params['prod_id']) || empty($arr_params['prod_id']) || 0 == $arr_params['prod_id']) ? 0 : $arr_params['prod_id'];
    $limit_records = (!isset($arr_params['limit_records']) || empty($arr_params['limit_records']) || 0 == $arr_params['limit_records']) ? 50 : $arr_params['limit_records'];
    $user_id = (!isset($arr_params['user_id']) || empty($arr_params['user_id']) || 0 == $arr_params['user_id']) ? 0 : $arr_params['user_id'];
    
    if ($user_id > 0 && $prod_id > 0)
    {
        $core = &getCoreInstance();
        $db = &$core->getProperty("db");

        //---- Check, exist this product in database
        $sql = "select count(*) from history_products where user_id = {$user_id} and product_id = {$prod_id}";
        
        if (0 == $db->getOne($sql))
        {
            // Insert record with history in database
            $sql = "insert into history_products (user_id, product_id, date_review)";
            $sql .= " values ({$user_id}, {$prod_id}, now())";
            
            $db->Exec($sql);
        }
        
        //------ Delete superfluous records
        $sql = "select count(*) from history_products where user_id = {$user_id}";
        if ($limit_records < ($count = $db->getOne($sql)))
        {
            $sql = "delete from history_products order by date_review asc limit " . ($count - $limit_records);
            $db->Exec($sql);
        }
        
        return true;
    }
    
    return false;
}


// ----------------------------------------------------------------------------------
// --- [ Insert history record ] -------------------------------------------------------------
// ----------------------------------------------------------------------------------

function getHistoryRecords($arr_params = array())
{
    /*$arr_params = array(
     * prod_id = 0;  if prod_id = 0 or empty - default select information all products
     * limit_records = 0; - limit get records from database; if limit_records = 0 or empty - default limit_records = 50
     * user_id = 0;  if user_id = 0 or empty - function do not work
     * )
     */


    $prod_id = (!isset($arr_params['prod_id']) || empty($arr_params['prod_id']) || 0 == $arr_params['prod_id']) ? 0 : $arr_params['prod_id'];
    $limit_records = (!isset($arr_params['limit_records']) || empty($arr_params['limit_records']) || 0 == $arr_params['limit_records']) ? 50 : $arr_params['limit_records'];
    $user_id = (!isset($arr_params['user_id']) || empty($arr_params['user_id']) || 0 == $arr_params['user_id']) ? 0 : $arr_params['user_id'];

    if ($user_id > 0)
    {
        $core = &getCoreInstance();
        $db = &$core->getProperty("db");

        //---- Get records with history products
        $sql = "";
        if ($prod_id > 0)
        {
            $sql = "select user_id, product_id, date_review";
            $sql .= " from history_products";
            $sql .= " where user_id = {$user_id} and product_id = {$prod_id} order by date_review desc" . ($limit_records > 0 ? " limit {$limit_records}" : "");
        }
        else
        {
            $sql = "select user_id, product_id, date_review";
            $sql .= " from history_products";
            $sql .= " where user_id = {$user_id} order by date_review desc" . ($limit_records > 0 ? " limit {$limit_records}" : "");
        }
        
        $arr_history = $db->getAll($sql);
        return ArrayDb2Str($arr_history);
    }
    
    return false;
}


}
?>